package com.amazon.dao;

import com.amazon.bo.Shop;
import com.amazon.dao.base.BaseDao;
import com.amazon.web.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class ShopDao extends BaseDao<Shop> {
    public Shop getShopByName(final String shopname){
        Shop user= execute(session -> session
                .createNativeQuery("SELECT * FROM shop WHERE shopname=:shopname ", Shop.class)
                .setParameter("shopname", shopname)
                .uniqueResult());
        return  user;
    }

    public List<Shop> getShopList(final int page, final int pageSize) {
        List<Shop> results = execute(session -> (List<Shop>) session
                .createNativeQuery("SELECT * FROM shop ", Shop.class)
                .setFirstResult((page - 1) * pageSize)
                .setMaxResults(pageSize)
                .getResultList());
        return results != null ? results : new ArrayList<Shop>();
    }

    public boolean saveShop(Shop shop){
        try {

                add(shop);
                return true;
        }
        catch (Exception ex)
        {return false;}

    }

    public Object selectShopList(long uid) {


       String sql="select shopname name,visitcode code from shop where user_id=:userId";

        Object list= execute(session -> {
            Query query = session.createNativeQuery(sql);
            query.setParameter("userId",uid);
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            return query.getResultList();
        });
        return list;
    }

    public Object selectShopInfo(long uid) {

        String sql="SELECT id,username loginname,code_type codeType,shop_code shopcode,shop_key shopkey,nickname shopname,deal_days dealdays,ratio,open_account  openaccount FROM user where id=:id";

        Object list= execute(session -> {
            Query query = session.createNativeQuery(sql);
            query.setParameter("id",uid);
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            return query.getResultList();
        });
        return list;

    }

    public Object getShopList(int userid,final int page, final int pageSize) {
        String sql=  "SELECT s.id,CONCAT(u.username,'(',s.shopname,')') AS shopaccount,CONCAT(u.real_name,'(',s.shopname,')') AS shopname, "+
                "s.codetype,s.ratio,s.dealdays,s.openaccount, s.service_url,s.shopkey,s.visitcode  ,s.loginname,u.mobile,s.shopstatus  "+
       "FROM   shop s LEFT JOIN  amazon.user  u ON s.user_id=u.id"
+ (userid==0?"":" WHERE s.user_id="+userid+" limit "+((page-1)*pageSize)+","+pageSize);

        Object list= execute(session -> {
            Query query = session.createNativeQuery(sql);
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            return query.getResultList();
        });
        return list;
    }
    //获店铺详情
    public Object getShopDetail(int shopid) {
        String sql=  "SELECT s.id,CONCAT(u.username,'(',s.shopname,')') AS shopaccount,CONCAT(u.real_name,'(',s.shopname,')') AS shopname, "+
                "s.codetype,s.ratio,s.dealdays,s.openaccount, s.service_url,s.shopkey,s.visitcode  ,s.loginname,u.mobile,s.shopstatus,u.created_on,u.real_name,s.shopcode  "+
                "FROM   shop s LEFT JOIN  amazon.user  u ON s.user_id=u.id "
                 +" where s.id="+shopid;

        Object o= execute(session -> {
            Query query = session.createNativeQuery(sql);
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            return query.getSingleResult();
        });
        return o;
    }


    public Object selectShopAll() {


        String sql="select id from shop";

        Object list= execute(session -> {
            Query query = session.createNativeQuery(sql);
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            return query.getResultList();
        });
        return list;
    }




}
